package com.krystal.controller;

import com.core.var.AgentMsg;
import com.core.var.Constants;
import com.krystal.crawler.Covid19DateCrawlerCountry;
import com.krystal.crawler.Covid19DateCrawlerImportCase;
import com.krystal.crawler.Covid19DateCrawlerMain;
import com.krystal.mapper.DataMapper;
import com.krystal.services.SpiderMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 爬虫数据控制类
 * @author Krystal1
 * @date 2022/1/16 16:54:56
 */
@Controller //@Controller+@ResponseBody = @RestController 表示该类是springboot的一个Controller，返回的数据为json格式
@ResponseBody
@RequestMapping("/crawler")
@Slf4j
public class DataController {

    /**数据库操作对象*/
    @Autowired
    private DataMapper dataMapper;
    /**更新covid19_1表的组件类*/
    @Autowired
    private Covid19DateCrawlerCountry covid19DateCrawlerCountry;
    /**更新covid19_0表的数据*/
    @Autowired
    private Covid19DateCrawlerImportCase covid19DateCrawlerImportCase;
    /**更新covid19_2/3/4表的数据*/
    @Autowired
    private Covid19DateCrawlerMain covid19DateCrawlerMain;

    /**
     * 执行爬虫更新covid19_1表数据 该表为中国自20201119以来到昨天为止的数据
     * @param request
     * @return Response
     */
    @RequestMapping("/V01/updateCovid19_1")
    public SpiderMsgService.Response updateCovid19_1(@RequestBody SpiderMsgService.Request request){
        SpiderMsgService.Response response = new SpiderMsgService.Response();
        try {
            long start = System.currentTimeMillis();
            response = covid19DateCrawlerCountry.crawlChinaData(request);
            long end = System.currentTimeMillis();
            int costs = (int) ((end - start)/1000);
            response.setUpdateCount(response.getUpdateCount() + "条数据");
            response.setCost("更新耗时" + costs + "秒！");
            log.info("处理成功！");
        } catch (Exception e) {
            log.info("处理失败！");
            response.setReturnCode(Constants.FAIL_CODE);
            response.setReturnMsg(AgentMsg.FAILED_MSG);
            response.setIsUpdated("0");
        }

        return  response;
    }

    /**
     * 执行爬虫更新covid19_0表数据 该表为中国近两个月的数据(包括境外输入病例等具体信息)  该接口 不需要传数据
     * @return Response
     */
    @RequestMapping("/V01/updateCovid19_0")
    public SpiderMsgService.Response updateCovid19_0(){
        SpiderMsgService.Response response = new SpiderMsgService.Response();
        try {
            long start = System.currentTimeMillis();
            response = covid19DateCrawlerImportCase.crawlImportCaseData();
            long end = System.currentTimeMillis();
            int costs = (int) ((end - start)/1000);
            response.setUpdateCount(response.getUpdateCount() + "条数据");
            response.setCost("更新耗时" + costs + "秒！");
            log.info("处理成功！");
        } catch (Exception e) {
            log.info("处理失败！");
            response.setReturnCode(Constants.FAIL_CODE);
            response.setReturnMsg(AgentMsg.FAILED_MSG);
            response.setIsUpdated("0");
        }

        return  response;
    }

    /**
     * 执行爬虫更新covid19_2/3/4表数据 这几个表是省份、城市对应的数据信息
     * @return Response
     */
    @RequestMapping("/V01/updateCovid19_234")
    public SpiderMsgService.Response updateCovid19_234(){
        SpiderMsgService.Response response = new SpiderMsgService.Response();
        try {
            long start = System.currentTimeMillis();
            //3.27修改日志： 手动清空表数据 再执行爬虫 直接插入数据
            dataMapper.truncateTable("covid19_4");
            log.info("已执行清空表covid19_4操作！");
            response = covid19DateCrawlerMain.crawlOthers();
            long end = System.currentTimeMillis();
            int costs = (int) ((end - start)/1000);
            response.setUpdateCount(response.getUpdateCount() + "条数据");
            response.setCost("更新耗时" + costs + "秒！");
            log.info("处理成功！");
        } catch (Exception e) {
            log.info("处理失败！");
            response.setReturnCode(Constants.FAIL_CODE);
            response.setReturnMsg(AgentMsg.FAILED_MSG);
            response.setIsUpdated("0");
        }

        return  response;
    }


    /**
     * 删除传入的表名                  注意慎重 调用此接口会删除整个表的记录
     * @param request
     * @return
     */
    @RequestMapping("/V01/truncateTable")
    public SpiderMsgService.Response truncateTable(@RequestBody SpiderMsgService.Request request){
        SpiderMsgService.Response response = new SpiderMsgService.Response();
        try {
            if("true".equals(request.getIsToTruncate())){
                dataMapper.truncateTable(request.getTableName());
                response.setReturnCode(Constants.SUCCESS_CODE);
                response.setReturnMsg(AgentMsg.TRUNCATE_SUCCESS);
                log.info("删除成功！");
            }
        } catch (Exception e) {
            log.info("处理失败！");
            response.setReturnCode(Constants.TRUNCATE_ERROR);
            response.setReturnMsg(AgentMsg.TRUNCATE_ERROR);
        }

        return  response;
    }






}
